package com.ugrow.ugrowcommunity.activity.moments.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.ugrow.ugrowcommunity.R;
import com.ugrow.ugrowcommunity.activity.moments.momentBean.Data;
import com.ugrow.ugrowcommunity.databinding.MomentsItemBinding;

import java.util.List;

public class MomentAdapter extends RecyclerView.Adapter<MomentAdapter.ViewHolder> {

    private List<Data> list;
    private Context context;
    private OnCommendClickListener onCommendClickListener;

    public MomentAdapter(List<Data> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.moments_item, parent, false));
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Glide.with(context).load(list.get(position).getUrl()).into( holder.bind.firstImage);
        holder.bind.title.setText("#" + list.get(position).getTitle());
        holder.bind.content.setText(list.get(position).getContent());
        holder.bind.likeNum.setText(String.valueOf(list.get(position).getCount()));
        holder.bind.like.setOnCheckedChangeListener((buttonView, isChecked) -> {
            int like = Integer.parseInt(holder.bind.likeNum.getText().toString());
            if (isChecked) {
                holder.bind.like.startAnimation(AnimationUtils.loadAnimation(context, R.anim.like_anim));
                holder.bind.likeNum.setText(String.valueOf(like + 1));
            } else {
                holder.bind.likeNum.setText(String.valueOf(like - 1));
            }
        });
        holder.bind.commentsBtn.setOnClickListener(v -> onCommendClickListener.onCommendClick(position));
        holder.bind.comments.setOnClickListener(v -> onCommendClickListener.onCommendClick(position));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        MomentsItemBinding bind;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            bind = MomentsItemBinding.bind(itemView);
        }
    }


    public void setOnCommendClickListener(OnCommendClickListener listener) {
        onCommendClickListener = listener;
    }


    public interface OnCommendClickListener {
        void onCommendClick(int position);
    }

    public void dispatchUpdate(List<Data> newList){
        List<Data> dataList = this.list;
        DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new DiffCallBack<>(dataList, newList), true);
        diffResult.dispatchUpdatesTo(this);
        list.clear();
        list.addAll(newList);
    }

}
